package site.eblog.filter;

import java.io.BufferedReader;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Enumeration;

import javax.servlet.ReadListener;
import javax.servlet.ServletContext;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

import org.aspectj.weaver.ast.Instanceof;

public class XSSServletRequest extends HttpServletRequestWrapper {
	private HttpServletRequest myRequest; // 用于保存读取body中数据

	public XSSServletRequest(HttpServletRequest request) throws IOException {
		super(request);
		myRequest = request;
	}

	@Override
	public BufferedReader getReader() throws IOException {
		return new BufferedReader(new InputStreamReader(getInputStream()));
	}

	@Override
	public String getParameter(String name) {
		String string = super.getParameter(name);
		// 返回值之前 先进行过滤
		return XssFilterUtil.stripXss(string);
	}

	@Override
	public String[] getParameterValues(String name) {
		// 返回值之前 先进行过滤
		String[] values = super.getParameterValues(name);
		if (values != null) {
			for (int i = 0; i < values.length; i++) {
				values[i] = XssFilterUtil.stripXss(values[i]);
			}
		}
		return values;
	}

	// 在这个方法过滤json数据
	@Override
	public ServletInputStream getInputStream() throws IOException {
		String isMultipart = myRequest.getContentType();
		if(isMultipart != null && isMultipart.indexOf("multipart") == -1){
			String bodyStr = getRequestPostStr(myRequest);
			bodyStr = XssFilterUtil.stripXss(bodyStr);// Xss过滤
		
			final ByteArrayInputStream bais = new ByteArrayInputStream(bodyStr.getBytes());
			return new ServletInputStream() {
	
				@Override
				public int read() throws IOException {
	
					return bais.read();
				}
	
				@Override
				public boolean isFinished() {
					// TODO Auto-generated method stub
					return false;
				}
	
				@Override
				public boolean isReady() {
					// TODO Auto-generated method stub
					return false;
				}
	
				@Override
				public void setReadListener(ReadListener arg0) {
					// TODO Auto-generated method stub
	
				}
			};
		}else{
			ServletInputStream fileStream = myRequest.getInputStream();
			return fileStream;
		}
	}

	/**
	 * 描述:获取 post 请求的 byte[] 数组
	 * 
	 * <pre>
	 * 举例：
	 * </pre>
	 * 
	 * @param request
	 * @return
	 * @throws IOException
	 */
	public static byte[] getRequestPostBytes(HttpServletRequest request) throws IOException {
		int contentLength = request.getContentLength();
		if (contentLength < 0) {
			return null;
		}
		byte buffer[] = new byte[contentLength];
		for (int i = 0; i < contentLength;) {

			int readlen = request.getInputStream().read(buffer, i, contentLength - i);
			if (readlen == -1) {
				break;
			}
			i += readlen;
		}
		return buffer;
	}

	/**
	 * 描述:获取 post 请求内容
	 * 
	 * <pre>
	 * 举例：
	 * </pre>
	 * 
	 * @param request
	 * @return
	 * @throws IOException
	 */
	public static String getRequestPostStr(HttpServletRequest request) throws IOException {
		byte buffer[] = getRequestPostBytes(request);
		String charEncoding = request.getCharacterEncoding();
		if (charEncoding == null) {
			charEncoding = "UTF-8";
		}
		return buffer == null || buffer.length <= 0 ? "" : new String(buffer, charEncoding);
	}
}
